// 在页面或组件中导入
import validate from 'wechat-validate'
Page({
  // 通过 behaviors 注入 validate 方法
  behaviors: [validate],
  data: {
    account:"",
    codes:"",
    code:""
  },
  rules: {
    account: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请检查手机号码是否正确!' },
    ],
    codes: [
      { required: true, message: '请填写短信验证码!' },
    ]
  },
  onLoad() {

  },
  // 获取验证码
  async getcode(){
    const { valid, dataKey, message } = this.validate('account')
    if(!valid) return wx.utils.toast({title: message, icon: 'none'})
      const _this = this
      wx.request({
        url: 'https://slwl-api.itheima.net/driver/register/captcha',
        method:'GET',
        data:{
          key:'1234567'
        },
        responseType:'arraybuffer',
        success (res) {
          console.log(res,333)
          _this.setData({
            codes:'data:image/png;base64,'+wx.arrayBufferToBase64(res.data)
          })
          console.log( wx.arrayBufferToBase64(res.data),4333)
        }
      })
      
    },
    // 登录
 async button(){
    // 验证码 rules 中定义的全部数据
    const isAllValid = this.validate()
    if(!isAllValid) return
    const res = await wx.http.post('/driver/register/account',{
      account:this.data.account,
      password:'123456',
      key:'1234567',
      code: this.data.code
    })
    if(res.code !== 200) return wx.utils.toast('注册失败')
    wx.navigateTo({
      url: '/pages/login/index',
    })
  }
})